package com.cakeclient;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.CatalogCategory;
import com.cakeclient.Model.MenuType;
import com.cakeclient.Model.ProductType;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


public class ClientProductsFragment extends SherlockFragment {

    private CatalogCategory thisIsRoot;

    private List<Long> childCategoriesId = new ArrayList<Long>();
    private List<Long> childProductsId = new ArrayList<Long>();
    private List<Long> childMenusId = new ArrayList<Long>();

    private List<CatalogCategory> childCategories = new ArrayList<CatalogCategory>();
    private List<ProductType> childProducts = new ArrayList<ProductType>();
    private List<MenuType> childMenus = new ArrayList<MenuType>();

    private List<Object> objectList = new ArrayList<Object>();
    private Stack<Long> categoryIdStack = new Stack<Long>();


    
    ListView listView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.client_catalog_products,container,false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.

        if (CatalogCategory.getOneFromRemoteID(getActivity().getBaseContext(), 1l) == null) {
            thisIsRoot = new CatalogCategory("Root", "Root Of Catalog !");
            thisIsRoot.saveToDatabase(getActivity().getBaseContext());
        } else {
            thisIsRoot = CatalogCategory.getOneFromRemoteID(getActivity().getBaseContext(), Long.valueOf(1));
        }

        listView = (ListView) getActivity().findViewById(R.id.catalog_listview);
        listView.setOnItemClickListener(lvListenner);

        getCategory(thisIsRoot.getIdDB(), true);


    }

    // Listenners

    private ListView.OnItemClickListener lvListenner = new ListView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            // On récupére l'objet sélectionné
            Object object = adapterView.getItemAtPosition(i);
            if (object instanceof CatalogCategory) {
                if (!object.equals(thisIsRoot)){
                    ((TextView)getActivity().findViewById(R.id.catalog_products_description_textview)).setText("Category: " + ((CatalogCategory) object).getName());
                }
                else {
                    ((TextView)getActivity().findViewById(R.id.catalog_products_description_textview)).setText("Products Catalog");
                }
                getCategory(((CatalogCategory) object).getIdDB(), true);
            } else if (object instanceof ProductType) {



            } else if (object instanceof MenuType) {

            } else {
                if (categoryIdStack.size() > 1) {
                    categoryIdStack.pop();
                    getCategory(categoryIdStack.lastElement(), false);
                } else {
                    Toast toast = Toast.makeText(getActivity().getBaseContext(), "You are already to the root of the catalog!", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }


        }
    };





    public void getCategory(Long dirPath, Boolean dir)

    {
        objectList.clear();
        childCategories.clear();
        childProducts.clear();
        childMenus.clear();


            // Ici on avance dans le catalogue
            categoryIdStack.add(dirPath);
        if (dirPath != thisIsRoot.getIdDB()) {
        // Ajout du retour
        objectList.add("Back");
        }
        // On récupére les objets à partir des ids
        childCategoriesId = CatalogCategory.getOne(getActivity().getBaseContext(), dirPath).getChildCategoriesId();
        for (Long l : childCategoriesId) {
            childCategories.add(CatalogCategory.getOne(getActivity().getBaseContext(), l));
        }
        childProductsId = CatalogCategory.getOne(getActivity().getBaseContext(), dirPath).getChildProductTypeId();
        for (Long l : childProductsId) {
            childProducts.add(ProductType.getOne(getActivity().getBaseContext(), l));
        }

        childMenusId = CatalogCategory.getOne(getActivity().getBaseContext(), dirPath).getChildMenuTypeId();
        for (Long l : childMenusId) {
            childMenus.add(MenuType.getOne(getActivity().getBaseContext(), l));
        }


        // Ajout des catégories.
        for (int i = 0; i < childCategories.size(); i++) {
            objectList.add(childCategories.get(i));
        }

        // Ajout des Menus.
        for (int i = 0; i < childMenus.size(); i++) {
            if (childMenus.get(i) != null)
                objectList.add(childMenus.get(i));
        }

        // Ajout des produits
        for (int i = 0; i < childProducts.size(); i++) {
            if (childProducts.get(i) != null)
                objectList.add(childProducts.get(i));
        }


        ListAdapter adapter = new ListviewClientCatalogAdapter(getActivity(), R.layout.client_listview_item_category, objectList);
        listView.setAdapter(adapter);

    }

}
